package bluetooth.lite.com.lightble;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.vise.baseble.BluetoothDeviceManager;
import com.vise.baseble.ViseBle;
import com.vise.baseble.callback.IConfigCallback;
import com.vise.baseble.callback.IConnectCallback;
import com.vise.baseble.core.DeviceMirror;
import com.vise.baseble.exception.BleException;
import com.vise.baseble.model.BluetoothLeDevice;
import com.vise.baseble.utils.BleUtil;

public class MainActivity extends AppCompatActivity {

    private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化蓝牙设备
        BluetoothDeviceManager.getInstance().init(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        checkBluetoothPermission();
    }

    @Override
    protected void onDestroy() {
        ViseBle.getInstance().clear();
        super.onDestroy();
    }

    /**
     * 检查蓝牙权限
     */
    private void checkBluetoothPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // Android M Permission check
            if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_COARSE_LOCATION:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // TODO request success
                }
                break;
        }
    }


    public void connectBLE(View view) {

        //监听连接状态
        BluetoothDeviceManager.getInstance().enableConnectCallback(new IConnectCallback() {
            @Override
            public void onConnectSuccess(DeviceMirror deviceMirror) {
                connectedSuccess(deviceMirror);
            }

            @Override
            public void onConnectFailure(BleException exception) {

            }

            @Override
            public void onDisconnect(boolean isActive) {

            }
        });

        //连接指定mac地址的设备
        BluetoothDeviceManager.getInstance().connectByMac("00:15:86:13:C8:4D");
    }

    private void connectedSuccess(DeviceMirror deviceMirror) {
        BluetoothLeDevice device = deviceMirror.getBluetoothLeDevice();
        BluetoothDeviceManager.getInstance().configDiscoverDevice(device, new IConfigCallback() {
            @Override
            public void onConfigComplete(boolean success) {
                if (success) {
                    //这里需要用主线程调用
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                            startActivity(intent);
                        }
                    });

                }
            }
        });
        
    }
}
